﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class autoData : System.Web.UI.Page
{
    public const string KEYWORD_REQUEST_KEY = "query";

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(getData());
    }

    private String getQuery()
    {
        Object obj = Request[KEYWORD_REQUEST_KEY];
        if (obj == null)
            return "";
        else
            return DataUtil.ObjetToString(obj);
    }

    private String getData()
    {
        ctpDataContext db = new ctpDataContext();
        StringBuilder sb = new StringBuilder();
        //TSET
        //String[] tests = { "John", "Jeff", "tim", "香港" };
        //var names = from n in tests
        //            where n.ToLower().Contains(getQuery().ToLower())
        //            select n;

        String key = getQuery().ToLower();
        var city_search = from p in db.ct_profiles
                          join q in db.ct_language_texts.Where(a => a.from_table_no == (int)DatabaseUtil.fromTables.ct_profile_title)
                          on p.no equals q.post_no
                          where p.title == key || q.text.Contains(key)
                          select new
                          {
                              result = q.text,
                              no = p.no,

                          };
        foreach(var city in city_search)
        {
            sb.Append(city.result + "\t" + city.no + "\n");
        }
        var spot_search = from p in db.ct_s_profiles
                          join q in db.ct_language_texts.Where(a => a.from_table_no == (int)DatabaseUtil.fromTables.s_profile_title)
                          on p.no equals q.post_no
                          where (p.title == key || q.text.Contains(key)) && p.valid == true
                          select new
                          {
                              result = q.text,
                              no = p.no
                          };
        foreach (var spot in spot_search)
        {
            sb.Append(spot.result + "\t" + spot.no + "\n");
        }
        return sb.ToString();
    }

}
